const axios = require('axios')
const fs = require('fs')


const baseURL = ''
// 创建 axios 实例
const MainHttp = axios.create({
    baseURL, // 设置基础URL
    timeout: 60000, // 设置请求超时时间
});

const downloadFile = async function (url, filePath, config = {}) {
    return await MainHttp.get(url, {
        responseType: 'stream',
        ...config
    })
        .then(res => {
            return new Promise((resolve, reject) => {
                res.data.pipe(fs.createWriteStream(filePath))
                    .on('finish', () => {
                        resolve()
                    })
                    .on('error', e => {
                        reject(e)
                    })
            })
        })
}

downloadFile('url',
    // "C:\\Users\\PC\\Documents\\测试\\123456_111\\test\\444\\projectData.xls", {
    "C:/Users/PC/Documents/测试/123456_111/test/444/projectData.xlsx", {
        params: {
    userName: 'xxx',
    password: 'xxx$',
    ContractNo: 'SCJSD20250218-Z01'
}}).then(() => {
    console.log('下载完成')
}).catch(e => {
    console.log(e)
})

